﻿using System;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;

namespace Sinbiota.Web.Controllers
{
    public class PublicationController : BaseController
    {
        private readonly IPublicationPersistenceServices _publicationPersistenceServices;

        public PublicationController(IPublicationPersistenceServices publicationPersistenceServices)
        {
            _publicationPersistenceServices = publicationPersistenceServices;
        }

        //
        // GET: /Publication/
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_publicationPersistenceServices.FindAll());
        }

        //
        // GET: /Publication/Details/5
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var publication = _publicationPersistenceServices.FindById(id);
            // Null, not found:
            if (publication == null)
            {
                return Request.IsAjaxRequest() ? Content(Strings.PageNotFound) : (ActionResult)View("NotFound");
            }
            // Found:
            return Request.IsAjaxRequest() ? (ActionResult)PartialView("DisplayTemplates/Publication", publication) : View(publication);
        }

        //
        // GET: /Publication/Create
        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Publication/Create
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(Publication publication, FormCollection collection)
        {
            var newPublication = new Publication();
            TryUpdateModel(newPublication);
            if (ModelState.IsValid)
            {
                _publicationPersistenceServices.Save(newPublication);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(publication);
        }

        //
        // GET: /Publication/Edit/5
        [ImportMessagesFromTempData]
        public ActionResult Edit(int id)
        {
            var publication = _publicationPersistenceServices.FindById(id);
            return publication == null ? View("NotFound") : View(publication);
        }

        //
        // POST: /Publication/Edit/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, Publication publication, FormCollection collection)
        {
            var editPublication = _publicationPersistenceServices.FindById(id);
            TryUpdateModel(editPublication);
            if (ModelState.IsValid)
            {
                _publicationPersistenceServices.Update(editPublication);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(publication);
        }

        //
        // GET: /Publication/Delete/5
        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var publication = _publicationPersistenceServices.FindById(id);
            return publication == null ? View("NotFound") : View(publication);
        }

        //
        // POST: /Publication/Delete/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deletePublication = _publicationPersistenceServices.FindById(id);
            if (deletePublication == null) return View("NotFound");
            _publicationPersistenceServices.Delete(deletePublication);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
        //AJAX:
        [HttpPost]
        public ActionResult AjaxDelete(int deleteId)
        {
            var deletePublication = _publicationPersistenceServices.FindById(deleteId);
            if (deletePublication == null) return Json(new { Success = false, Message = Strings.NotFound });
            _publicationPersistenceServices.Delete(deletePublication);
            return Json(new { Success = true, Message = Messages.OperationCompletedSuccessfully});
        }
    }
}
